// pages/home/myGift/myGift.js
var app = getApp()
import Index from '../../../api/index';
import orderIndex  from '../../../api/order';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headerHeight:app.globalData.headerHeight,
    type:1,
    pageNo:1,
    pageSize:10,
    orderPrizeList:[],
    orderList:[],
    isAllSelect:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
//订单
getOrder(e){
  var type=e.currentTarget.dataset.type
  this.setData({
    type:type,
    pageNo:1,
  })
  this.getOrderList()
},
//查看物流
getlogistics(e){
  var item=e.currentTarget.dataset.item
  wx.navigateTo({
    url: `/pages/user/logistics/logistics?orderNo=${item.orderNo}&&type=3`,
  })
},
getOrderList(){
  if(this.data.type==1){
    Index.apiMarketingactivityUnsentPrize().then(res=>{
      var orderPrizeList=res.data.map(row=>{
        row.productImgs=row.img.split(',')
        return row
      })
      this.setData({
        orderPrizeList:orderPrizeList,
      })
    })
  }else{
    var opt={
      pageNo:this.data.pageNo,
    pageSize:this.data.pageSize,
    }
    Index.apiMarketingactivitySendOrder(opt).then(res=>{
      if(opt.pageNo==1){
        var orderList=res.data.map(res=>{
          res.productImgs=res.productImgs.split(',')
          return res
        })
      }else{
        var dataList=res.data.map(res=>{
          res.productImgs=res.productImgs.split(',')
          return res
        })
        var orderList=this.data.orderList.concat(dataList)
      }
      var isMore=false
      if(res.data.length<opt.pageSize){
        isMore=true
      }
      this.setData({
        orderList:orderList,
        isMore:isMore,
      })
    })
  }
},
getSelectAll(){
  var isAllSelect=this.data.isAllSelect
  var orderPrizeList=this.data.orderPrizeList.map(res=>{
    res.isSelect=!isAllSelect?true:false
    return res
  })
  this.setData({
    orderPrizeList:orderPrizeList,
    isAllSelect:!isAllSelect,
  })
},
getSelect(e){
  var id= e.currentTarget.dataset.id
  var orderPrizeList=this.data.orderPrizeList.map(res=>{
    if(res.id==id){
      res.isSelect=!res.isSelect
    }
    return res
  })
  this.setData({
    orderPrizeList:orderPrizeList,
    isAllSelect:this.areAllValuesEmpty(orderPrizeList),
  })
},

areAllValuesEmpty(arr) {
  return arr.every(item => item.isSelect);
},

//申请发货
ApplyShipment(){
  var data=[]
  this.data.orderPrizeList.forEach(res=>{
    if(res.isSelect){
      data.push(res)
    }
  })
  if(data.length==0){
    wx.showToast({
      title: '请先选择发货的商品',
      icon:'none'
    })
    return false
  }
  wx.setStorageSync('giftList', data)
  wx.navigateTo({
    url: '/pages/home/giftOrderConfirm/giftOrderConfirm',
  })
},

confirmReceiptGoods(e){
  var item=e.currentTarget.dataset.item
  var opt={
    orderNo:item.orderNo
  }
  var that=this
  wx.showModal({
    title: '提示',
    content: '请先确认已经收到货',
    success (res) {
      if (res.confirm) {
        orderIndex.apiConfirmReceipt(opt).then(res=>{
          wx.showToast({
            title:'确认收货成功',
              icon: 'none',
              duration:3000
            }) 
            that.setData({
              pageNo:1
            })
            that.getOrderList()
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      pageNo:1,
      isAllSelect:false,

    })
    this.getOrderList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})